package com.logisticsmanagement.activity;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.logisticsmanagement.R;
import com.logisticsmanagement.admin_pro.Admin_provinceActivity;
import com.logisticsmanagement.distribution_site.Admin_DistributionSiteActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 登录
 * @author 陈朝辉
 * @createTime 2015年3月18日 下午3:08:49
 * @version 1.0
 */

public class LoginActivity extends Activity {

	// 定义文本和按钮控件
	private EditText txtLoginUserName;
	private EditText txtLoginPassword;
	private TextView tvRegister;
	private Button btnLogin;
	private Button btnQuit;
	private Handler handler;
	private boolean flag = false;// 判断线程是否结束
	private Intent intent;
	private JSONObject jsonObject;
	private String userNameFromServer;
	private String passwordFromServer;
	private String roleFromServer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// 通过findviewbyid获取XML文件中的控件
		txtLoginUserName = (EditText) findViewById(R.id.txtLoginUserName);
		txtLoginPassword = (EditText) findViewById(R.id.txtLoginPassword);
		btnLogin = (Button) findViewById(R.id.btnLoginLogin);
		btnQuit = (Button) findViewById(R.id.btnLoginQuit);
		tvRegister = (TextView) findViewById(R.id.tvLoginIfRegister);

		// 给退出按钮设置监听器
		btnQuit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(
						LoginActivity.this);
				builder.setTitle("提示");
				builder.setMessage("您确定要退出程序吗？");
				// 设置确定按钮
				builder.setNegativeButton("确定",
						new AlertDialog.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int index) {
								// TODO Auto-generated method stub
								dialog.dismiss();
								LoginActivity.this.finish();
							}
						});
				// 设置取消按钮
				builder.setPositiveButton("取消",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface dialogInterface, int index) {
								// TODO Auto-generated method stub
								dialogInterface.dismiss();

							}
						});
				builder.create().show();

			}

		});
		
		//为"注册"添加点击监听器
		tvRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
				startActivity(intent);
			}
		});

		//给登录按钮设置监听器
		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				intent = new Intent(LoginActivity.this, Admin_provinceActivity.class);
				startActivity(intent);
				
				// 获取文本域中的字符
//				String userName = txtLoginUserName.getText().toString().trim();
//				String password = txtLoginPassword.getText().toString().trim();
//
//				if ("".equals(userName) || "".equals(password)) {
//					Toast.makeText(LoginActivity.this, "用户名或密码不能为空",
//							Toast.LENGTH_LONG).show();
//				}
//
//				if (flag = false) {
//					new LoginThread(userName, password).start();
//				}
//				try {
//					Bundle bundle = new Bundle();
//					String userInfo = bundle.getString("userInfo");
//					jsonObject = new JSONObject(userInfo);
//					JSONArray jsonArray = jsonObject.getJSONArray("jsonObject");
//					String string = jsonArray.get(0).toString();
//					JSONObject object = new JSONObject(string);
//					userNameFromServer = object.getString("displayName");
//					passwordFromServer = object.getString("password");
//					roleFromServer = object.getString("role");
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//
//				if (userName.equals(userNameFromServer)
//						&& password.equals(passwordFromServer)) {
//					if ("generalAdmin".equals(roleFromServer)) {
//						intent = new Intent(LoginActivity.this,
//								Admin_provinceActivity.class);
//						startActivity(intent);
//					} else if ("companyUser".equals(roleFromServer)) {
//						intent = new Intent(LoginActivity.this,
//								User_ProvinceActivity.class);
//						startActivity(intent);
//					} else if ("deliveryStationAdmin".equals(roleFromServer)) {
//						intent = new Intent(LoginActivity.this,
//								Admin_DistributionSiteActivity.class);
//						startActivity(intent);
//					} else if ("driver".equals(roleFromServer)) {
//						intent = new Intent(LoginActivity.this,
//								DriverActivity.class);
//						startActivity(intent);
//					} else {
//						intent = new Intent(LoginActivity.this,
//								NormalUserActivity.class);
//						startActivity(intent);
//					}
//				} else {
//					Toast.makeText(LoginActivity.this, "用户名或密码错误",
//							Toast.LENGTH_LONG).show();
//				}
			}
		});

//		// 回调处理
//		handler = new Handler() {
//			@Override
//			public void handleMessage(Message msg) {
//				Bundle bundle = msg.getData();
//				String userInfo = bundle.getString("userInfo");
//				if ("null".equals(userInfo)) {
//					Toast.makeText(LoginActivity.this, "用户名或密码错误!",
//							Toast.LENGTH_LONG).show();
//				}
//			}
//		};
//	}

	/**
	 * 登录访问服务器线程，防止系统假死
	 * 
	 * @author 陈朝辉
	 * @createTime 2015年3月18日 下午3:11:00
	 * @version 1.0
	 */
//	public class LoginThread extends Thread {
//
//		private String userName;
//		private String password;
//
//		public LoginThread(String userName, String password) {
//			this.userName = userName;
//			this.password = password;
//		}
//
//		public void run() {
//			try {
//				// 创建HTTP客户端对象
//				HttpClient httpClient = new DefaultHttpClient();
//				// 创建HTTP post对象
//				HttpPost httpPost = new HttpPost();
//				// 打开连接
//
//				// 创建键值对集合
//				List<NameValuePair> params = new ArrayList();
//				// 添加参数
//				params.add(new BasicNameValuePair("userName", userName));
//				params.add(new BasicNameValuePair("password", password));
//				// 设置HTTP实体
//				HttpEntity httpEntity = new UrlEncodedFormEntity(params,
//						HTTP.UTF_8);
//				httpPost.setEntity(httpEntity);
//				// 访问服务器，返回response对象
//				HttpResponse httpResponse = httpClient.execute(httpPost);
//				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//					// 获得返回的参数实体
//					httpEntity = httpResponse.getEntity();
//					// 将返回的数据转换成字符串
//					String userInfo = EntityUtils.toString(httpEntity);
//					// 定义绑定对象，绑定数据
//					Bundle bundle = new Bundle();
//					bundle.putString("userInfo", userInfo);
//					// 创建消息对象
//					Message msg = new Message();
//					msg.setData(bundle);
//					// 发送信息，执行回调函数
//					handler.sendMessage(msg);
//
//					flag = true;
//				}
//			} catch (UnsupportedEncodingException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (ClientProtocolException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}

}
